﻿using System;
using System.Management;

namespace ClusterAwareProperties
{
    /// <summary>
    /// Convert a Node ManagementObject to Node Class
    /// </summary>
    public static class NodeTranslate
    {
        /// <summary>
        /// Convert Node ManagementObject to Node Class
        /// </summary>
        /// <param name="clsnode">Node class</param>
        /// <param name="queryObj">ManagementObject Group</param>
        /// <returns>Node class with properties converted</returns>
        public static NodeProperty Convert(this NodeProperty clsnode, ManagementObject queryObj)
        {
            clsnode.BuildNumber = queryObj.PropertyValue<UInt32>("BuildNumber");
            clsnode.Caption = queryObj.PropertyValue<string>("Caption");
            clsnode.Characteristics = queryObj.PropertyEnum<UInt32, CharacteristicsNode>("Characteristics", CharacteristicsNode.Unknown);
            clsnode.CreationClassName = queryObj.PropertyValue<string>("CreationClassName");
            clsnode.CsdVersion = queryObj.PropertyValue<UInt32>("CsdVersion");
            clsnode.Dedicated = queryObj.PropertyArrayEnum<UInt16, NodeDedicated>("Dedicated");
            clsnode.Description = queryObj.PropertyValue<string>("Description");
            clsnode.EnableEventLogReplication = queryObj.PropertyValue<UInt32?>("EnableEventLogReplication");
            clsnode.Flags = queryObj.PropertyValue<UInt32>("Flags");
            clsnode.IdentifyingDescriptions = queryObj.PropertyValue<string[]>("IdentifyingDescriptions");
            clsnode.InitialLoadInfo = queryObj.PropertyValue<string[]>("InitialLoadInfo");
            clsnode.InstallDate = queryObj.PropertyValue<DateTime?>("InstallDate");
            clsnode.LastLoadInfo = queryObj.PropertyValue<String>("LastLoadInfo");
            clsnode.MajorVersion = queryObj.PropertyValue<UInt32>("MajorVersion");
            clsnode.MinorVersion = queryObj.PropertyValue<UInt32>("MinorVersion");
            clsnode.Name = queryObj.PropertyValue<string>("Name");
            clsnode.NameFormat = queryObj.PropertyEnum<UInt32, NameFormat>("NameFormat", NameFormat.NotRulesmapped);
            clsnode.NodeHighestVersion = queryObj.PropertyValue<UInt32>("NodeHighestVersion");
            clsnode.NodeInstanceId = queryObj.PropertyValue<string>("NodeInstanceId");
            clsnode.NodeLowestVersion = queryObj.PropertyValue<UInt32>("NodeLowestVersion");
            clsnode.OtherIdentifyingInfo = queryObj.PropertyValue<string[]>("OtherIdentifyingInfo");
            clsnode.PowerManagementCapabilities = queryObj.PropertyArrayEnum<UInt16, PowerCapacity>("PowerManagementCapabilities");
            clsnode.PowerManagementSupported = queryObj.PropertyValue<bool?>("PowerManagementSupported");
            clsnode.PowerState = queryObj.PropertyEnum<UInt16, PowerState>("PowerState", PowerState.Unknown);
            clsnode.PrimaryOwnerContact = queryObj.PropertyValue<string>("PrimaryOwnerContact");
            clsnode.PrimaryOwnerName = queryObj.PropertyValue<string>("PrimaryOwnerName");
            clsnode.PrivateProperties = new PrivatePropertiesData(queryObj);
            clsnode.ResetCapability = queryObj.PropertyEnum<UInt16, NodeResetCapability>("ResetCapability", NodeResetCapability.Unknown);
            clsnode.Roles = queryObj.PropertyValue<string[]>("Roles");
            clsnode.State = queryObj.PropertyEnum<UInt32, NodeState>("State", NodeState.Unknown);
            clsnode.Status = queryObj.PropertyEnum<string, StatusResource>("Status", StatusResource.Unknown);
            return clsnode;
        }

        ///<summary>
        /// Convert Node class to ManagementObject and Save on Cluster
        ///</summary>
        ///<param name="clsnode">Network class to converter</param>
        ///<param name="opSystem">Operation System where cluster is running</param>
        public static void SaveManagementObject(this NodeProperty clsnode, WindowsOperationSystem opSystem)
        {
            clsnode.Instance["Characteristics"] = (UInt32)clsnode.Characteristics;
            clsnode.Instance["Description"] = clsnode.Description;
            clsnode.Instance["Flags"] = clsnode.Flags;
            if (opSystem == WindowsOperationSystem.Windows2008 || opSystem == WindowsOperationSystem.Windows2008R2)
            {
                clsnode.Instance["PrivateProperties"] = clsnode.PrivateProperties.ConvertToManagemenBasetObject();
            }
            clsnode.Instance["Roles"] = clsnode.Roles;
            if (opSystem == WindowsOperationSystem.Windows2003 || opSystem == WindowsOperationSystem.Windows2003R2)
            {
                if (clsnode.EnableEventLogReplication != null)
                {
                    clsnode.Instance["EnableEventLogReplication"] = clsnode.EnableEventLogReplication;
                }
            }
            clsnode.Instance.Put();
        }
    }
}
